


function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    };
    return 0;
 }

console.log(getQueryString("ts_id"));
console.log("get amazon sub cate");
console.log(getQueryString("auto_get"))
var cate = getQueryString("ts_id");
if(!cate){
   var rh = decodeURIComponent(getQueryString('rh'));
    const regex = /n:([\d-]+)/;
    const match = rh.match(regex);
    if (match) {
       cate = match[1].replace('-', '');
    }
}
console.log("amazon_cate_"+cate);

console.log(getQueryString("auto_get")=='true' || getQueryString("ts_id") > 0)
//抓热效
if ($('input[name="ref"][value="cs_503_search"]').length) {
    console.log('503 拦截 sleep');
    setTimeout(() => {
           location.reload()
    },100000);
} else if(getQueryString("auto_get")=='true' || getQueryString("ts_id") > 0){

    var data = [];
    console.log("get data start");
    console.log( $(".s-card-container").html());
    var data = [];
     $(".s-card-container").each(function(e){
            if($(this).find(".puis-label-popover-default").html()){
                return;
            }
            console.log("index--");
            var indexid=$(this).attr("id")+"";
            var dataItem = {};
            dataItem.url = $(this).find("a").attr("href");
            dataItem.title = $(this).find(".a-size-medium ").html();
            if(!dataItem.title){
                dataItem.title =  $(this).find(".a-size-base-plus").html();
            }
            dataItem.price = $(this).find(".a-offscreen").html();
            dataItem.img = $(this).find("img").attr("src");
            dataItem.comment = $(this).find(".a-size-base.s-underline-text").html();
            dataItem.amazon_cate = cate;
            console.log(dataItem);
            data.push(dataItem);
     });
     console.log(data);
     console.log(data.length );
     var fetchNextCate = false;
     if(data && data.length > 0){
      console.log("have data ");
        var type= "getamazon";
        var q = getQueryString("k");
        var page = parseInt(getQueryString("page"));
        if(page<=0){
            page = 1;
        }
        console.log("q:"+q);
        chrome.runtime.sendMessage({ data: {type,data,page,q}, close: false ,skip_fetch_detail:true});
        if(page < 5){
            setTimeout(() => {
                // 获取当前URL
                var url = window.location.href;
                // 判断 URL 是否包含 page=xx
                if (url.includes('page=')) {
                  // 使用正则表达式替换字符串
                  url = url.replace(/(page=)(\d+)/g, function(match, p1, p2) {
                    return p1 + (page+1); // 将页码加 1
                  });
                } else {
                  // 拼接 page=1
                  url += (url.includes('?') ? '&' : '?') + 'page=2';
                }

                // 将参数列表拼接为新的 URL
                console.log(url);
                // 使用新的 URL 进行页面跳转
                location.href = url;
            },10000);
        }else{
            fetchNextCate = true;
        }

     }else{
        fetchNextCate = true;
     }
     if(fetchNextCate==true){
          console.log("fetchNextCate ");
          var type= "getamazonsubcateend";
          var q = getQueryString("k");

            console.log(cate); // Output: 1292115011
            if (cate) {
                setTimeout(function() {
                      chrome.runtime.sendMessage({ data: {type,data,page,q,cate}, close: true });
                }, 10000); // 20秒钟后关闭标签页

            }else{
                alert("cate error")
            }

     }
       /* chrome.runtime.onMessage.addListener(async (msg, sender) => {
            console.log(" jump page ")
            console.log(msg)
            if(msg && parseInt(msg) > 50){
                var type= "getamazonend";
                var q = getQueryString("k");
                chrome.runtime.sendMessage({ data: {type,data,page,q}, close: true });
                return;
            }
            // 获取当前URL
                var url = window.location.href;
                // 判断 URL 是否包含 page=xx
                if (url.includes('page=')) {
                  // 使用正则表达式替换字符串
                  url = url.replace(/(page=)(\d+)/g, function(match, p1, p2) {
                    return p1 + (msg); // 将页码加 1
                  });
                } else {
                  // 拼接 page=1
                  url += (url.includes('?') ? '&' : '?') + 'page=2';
                }
                console.log("jump_to_url_"+url);
                location.href = url;
        });*/
}



